package com.example.computer_exam_training_sys.service.impl;

import com.example.computer_exam_training_sys.entity.TopicCommentInfo;
import com.example.computer_exam_training_sys.mapper.TopicCommentMapper;
import com.example.computer_exam_training_sys.service.TopicCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TopicCommentServiceImpl implements TopicCommentService {
    @Autowired
    private TopicCommentMapper commentMapper;

    @Override
    public int addComment(TopicCommentInfo comment) {
        return commentMapper.insertComment(comment);
    }

    @Override
    public int deleteComment(Integer commentId) {
        return commentMapper.deleteCommentById(commentId);
    }

    @Override
    public Map<String, Object> getCommentsByTopicId(Integer topicId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<TopicCommentInfo> comments = commentMapper.getCommentsByTopicId(topicId);
        PageInfo<TopicCommentInfo> pageInfo = new PageInfo<>(comments);

        Map<String, Object> result = new HashMap<>();
        result.put("list", comments);
        result.put("total", pageInfo.getTotal());
        return result;
    }

    @Override
    public List<Map<String, Object>> getCommentsTitlesById(List<Integer> topicId) {
        return commentMapper.getTopicTitlesById(topicId);
    }

    @Override
    public Map<String, Object> getCommentsByUserId(Integer userId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<TopicCommentInfo> comments = commentMapper.getCommentsByUserId(userId);
        PageInfo<TopicCommentInfo> pageInfo = new PageInfo<>(comments);

        Map<String, Object> result = new HashMap<>();
        result.put("list", comments);
        result.put("total", pageInfo.getTotal());
        return result;
    }
}